<?php
class Malbum extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	function initAlbum($uid){
		$name = "默认专辑";
		$sql = " select * from tbalbum
				where uid = ? and name = ?";
		$query = $this->db->query($sql,array("$uid","$name"));
		$exists = false;
		foreach ($query->result() as $row){
			$exists = true;
		}
		if($exists == false){
			$sql = "insert into tbalbum(uid,name,`desc`,createtime)
					values(?,?,'',now())";
			$this->db->query($sql,array("$uid","$name"));
		}		
	}
	function checkExistsName($uid,$name){
		$sql = "select *
				from tbalbum
				where uid = ? and name = ?";
		$query = $this->db->query($sql,array("$uid","$name"));
		foreach ($query->result() as $row){
			return true;
		}
		return false;
	}
	
	function saveAlbumName($uid,$name){
		$sql = "insert into tbalbum(uid,name,`desc`,createtime) values(?,?,'',now())";
		$this->db->query($sql,array("$uid","$name"));
		return $this->db->affected_rows();
	}
	function loadAlbums($uid){
		$sql = "select *
				from tbalbum
				where uid=? and status=1
				order by id";
		$query = $this->db->query($sql,array("$uid"));
		return $query->result();
	}
	function delAblums($id){
		$sql = " update tbalbum
				set status = 0
				where id = ?";
		$this->db->query($sql,array("$id"));
		
		$sql = "update tbalbumdetail 
				set status = 0
				where albumid = ?";
		$this->db->query($sql,array("$id"));
				
	}
	function loadAlbumsDeleted($uid){
		$sql = "select *
				from tbalbum
				where uid = ? and status = 0 ";		
		$query = $this->db->query($sql,array("$uid"));
		return $query->result();
	}
	function restoreAblum($id){
		$sql = " update tbalbumdetail
				set  status = 1
				where albumid = ? ";
		$this->db->query($sql,array("$id"));
		
		$sql = " update tbalbum
				set  status = 1 
				where id = ? ";
		$this->db->query($sql,array("$id"));
	}
	function deleteAlbumDeep($id){
		$sql = " delete from tbalbumdetail where albumid = ?";
		$this->db->query($sql,array("$id"));
		
		$sql = " delete from tbalbum				
				where id = ? ";
		$this->db->query($sql,array("$id"));
	}
	
	
}
?>